using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.IO;
using System.Net;
namespace Test {
	class Pejcz {
		public static string FixPeiods(Match match) {
			return match.Groups[1].Value+" "+match.Groups[2].Value;
		}
		static void MainX() {
			List<string> all=new List<string>();
			Cache cache=new Cache("pej");
			Settings s=new Settings("pej");
			string cont=cache.Get(s["rss"],10);
			Nod nod=new Nod(cont);
			string[] items=nod.Doubles("<item>");
			string f1="<fieldset><legend>{0}</legend><img src=\"{1}\"/>{2}</fieldset>\r\n";
			bool upload=false;
			foreach(string item in items)
			{
				Nod itmNode=new Nod(item);
				string link=itmNode.Regex("<link>(.+?)\\?.+?</link>")[0];
				string id=new Regex(".+?-a(\\d+?)$").Match(link).Groups[1].Value;
				string path="C:\\img\\"+id+".jpg";
				string imgSml=itmNode.Regex("src=\"(http:.+?jpg)\"")[0];
				if(!File.Exists(path))
				{
					new WebClient().DownloadFile(imgSml,path);
					upload=true;
				}
				string artPage=cache.Get(link,60*24*10);
				Nod nodArt=new Nod(artPage);
				string stamp=nodArt.Regex("<span class=\"date\" timestamp=\"(\\d+?)\">")[0];
				DateTime dat=new DateTime(1970,1,1).AddSeconds(int.Parse(stamp));
				string tags=string.Join(",",new Nod(nodArt.Doubles("<p class=\"tags\">")[0]).Regex("<a.+?>(.+?)</a>")).ToLower();
				string img=nodArt.Regex("<div class=\"see-big\">.+?<img src=\"(.+?)\".+?/>")[0];
				string title=HttpUtility.HtmlDecode(Regex.Replace(nodArt.Doubles("<h1 class=\"title\">")[0],"<.+?>","").Trim());
				string art0=nodArt.Doubles("<div class=\"text clearfix\">")[0];
				string art1=HttpUtility.HtmlDecode(Regex.Replace(art0,"<.+?>|[\t\r\n]",""));
				//napraw kropki, zignoruj ..., wlacz inne znaki unicode
				string art2=Regex.Replace(art1,"([\\.\\?\\!])([A-Z])",FixPeiods);
				string art3=Regex.Replace(art2,"\\s{2,}"," ");
				all.Add(string.Format(f1,title,imgSml,art3));
			}
			if(upload)
			{
				File.WriteAllText("C:\\img\\pejcz.txt",string.Concat(all.ToArray()));
				FtpFile(s["ftp"],"C:\\img\\pejcz.txt",s["uid"],s["pwd"]);
			}
		}
		static void FtpFile(string FTPAddress,string filePath,string username,string password) {
			//Create FTP request
			FtpWebRequest request=(FtpWebRequest)FtpWebRequest.Create(FTPAddress+"/"+Path.GetFileName(filePath));
			request.Method=WebRequestMethods.Ftp.UploadFile;
			request.Credentials=new NetworkCredential(username,password);
			request.UsePassive=true;
			request.UseBinary=true;
			request.KeepAlive=false;
			//Load the file
			//FileStream stream=File.OpenRead(filePath);
			//byte[] buffer=new byte[stream.Length];
			//stream.Read(buffer,0,buffer.Length);
			//stream.Close();
			//Upload file
			byte[] buffer=File.ReadAllBytes(filePath);
			Stream reqStream=request.GetRequestStream();
			reqStream.Write(buffer,0,buffer.Length);
			reqStream.Close();
		}
		private static void Upload(string ftpServer,string userName,string password,string filename) {
			using(System.Net.WebClient client=new System.Net.WebClient())
			{
				client.Credentials=new System.Net.NetworkCredential(userName,password);
				client.UploadFile(ftpServer+"/"+new FileInfo(filename).Name,"STOR",filename);
			}
		}
	}
}
